var pcApp = function (win, option, Callback) {

    //alert(1)
    option = option || {};

    var vue = {};
    Callback = Callback || function () {};

    var _pwa_sw = require('./sw-drive.js');


    var axios = require('./ajax');

    var Fullscreen = require('./Fullscreen');


    var notify = require('./notify');

    this.setvue = function (val) {
        vue = val;
    };
    this.get = function (self, callback) {
        Callback(self)
        this.setvue(self);
        notify(self);
        var Axios = axios(self);
        self.$Ajax.setAxios(Axios);
        self.$WSPools.message("remote-login", function (db) {
            self.$message.error(self.$getTitle("@sys_remote_landing"));
            self.$router.push("/login");
        });
        var value = function (window, document, self) {
            var getmain = function (callback) {



                self.$UserJurisdiction.get(function (db) {
                    openMenu(false)
                });
                var openMenu = function (off) {
                    //self.$WSPools.init({ socket_id: db.socket_id });

                    console.log(off);

                    Axios(self.$Uris.Monitor, {})
                        .then(function (response) {
                            if (response.data) {
                                var db = response.data;
                                if (db.error > 0) {
                                    self.$Loading.hide();
                                    self.$router.push('/login');
                                } else {
                                    if (db.socket_id) {
                                        self.$WSPools.init({
                                            socket_id: db.socket_id
                                        });
                                    }
                                    if (off) {
                                        setmenu({
                                            name: db.name,
                                            icon: db.icon,
                                            items: db.jurisdictions
                                        })
                                    } else {
                                        self.$Loading.hide();
                                        self.UIEventPool.run("Sys_SideBar", "setDataList", db.jurisdictions);
                                    }
                                }
                            }
                        })
                };

                var collapse = 'false' != self.$getMeta('Collapse');

                if (localStorage.getItem('ms_collapse')) {
                    collapse = localStorage.getItem('ms_collapse') != 'false'
                }




                var getlayoutname = function () {
                    if (collapse) {
                        return "@sys_layout_default"
                    }
                    return "@sys_layout_collapse"
                }

                var setmenu = function (obj) {
                    var FileExplorer = {
                        name: "dialog",
                        customClass: "FileExplorer",
                        Config: {
                            SelectOk: function () {
                                FileExplorer.InfoFormVisible = false;
                            }
                        },
                        condition: {},
                        init: false,
                        InfoFormVisible: false,
                        fullscreen: true,
                        /*appendToBody: true,*/
                        modal: false,
                        setFileList: function (val) {
                            FileExplorer.condition = val;
                        },
                        title: "Sys_FileExplorer"
                    };

                    self.$FileExplorer.init(FileExplorer, self);

                    icon = obj.icon || 30;

                    var mainDrive = {
                        "InitVal": 0,
                        "name": "container",
                        "List": {
                            FileExplorer: FileExplorer,
                            "header": {
                                "InitVal": 0,
                                "name": "header",
                                "height": "70px",
                                "Form": {
                                    "title": self.$getMeta('title'),
                                    "InitVal": 0,
                                    "name": "headerbar",
                                    "Command": function (obj, self) {
                                        var command = obj.value
                                        if (command == 'loginout') {

                                            self.$Loading.show();
                                            self.$Ajax(self.$Uris.Signout, {}).then(function (response) {
                                                if (response.data && response.data.error == 0) {
                                                    setTimeout(() => {
                                                        self.$Loading.hide();
                                                        self.$router.push('/login');
                                                    }, self.TimeInterval);
                                                } else {
                                                    setTimeout(() => {
                                                        self.$Loading.hide();
                                                        self.$router.push('/login');
                                                    }, self.TimeInterval);
                                                }
                                            }).catch(function (error) {

                                                self.$Loading.hide();
                                            });

                                        } else if (command == "layout") {

                                            collapse = !collapse;

                                            mainDrive.List.container.List.aside.Form.collapse = collapse;
                                            mainDrive.List.container.List.aside.width = (collapse ? "70px" : "256px");


                                            localStorage.setItem('ms_collapse', collapse)

                                            self.Menu.List[2].label = getlayoutname();

                                        } else if (command == 'RestartBtn') {
                                            self.$router.push('/restart');
                                        } else if (command == 'Close') {
                                            window.close();
                                        } else if (command == 'Fullscreen') {
                                            Fullscreen(self);
                                        }
                                    },
                                    "Click": function (self) {
                                        try {
                                            var off = Fullscreen();
                                            self.Menu.List[0].visible = !off;
                                            self.Menu.List[1].visible = off;
                                        } catch (exception) {

                                        } finally {

                                        }
                                    },
                                    "Menu": {
                                        icon: {
                                            FontIcon: "el-icon-setting",
                                            id: icon,
                                        },
                                        tag: {
                                            type: 'success',
                                            effect: "dark",
                                            title: obj.name
                                        },
                                        "List": [{
                                                value: "Fullscreen",
                                                visible: true,
                                                disabled: false,
                                                label: "@sys_Fullscreen"
                                            }, {
                                                value: "Fullscreen",
                                                visible: false,
                                                disabled: false,
                                                label: "@sys_Exit_Fullscreen"
                                            }, {
                                                value: "layout",
                                                visible: true,
                                                disabled: false,
                                                label: getlayoutname()
                                            },
                                            {
                                                value: "RestartBtn",
                                                visible: true,
                                                disabled: false,
                                                label: "@sys_RestartBtn"
                                            }
                                        ].concat([{
                                            value: "loginout",
                                            visible: true,
                                            disabled: false,
                                            label: "@sys_loginout"
                                        }, {
                                            value: "Close",
                                            visible: true,
                                            disabled: false,
                                            label: "@sys_Close"
                                        }])
                                    }
                                }
                            },
                            "container": {
                                "InitVal": 0,
                                "name": "container",
                                "List": {
                                    "aside": {
                                        "InitVal": 0,
                                        "name": "aside",
                                        "width": collapse ? "70px" : "256px",
                                        "Form": {
                                            "InitVal": 0,
                                            "ID": "Sys_SideBar",
                                            "name": "sidebar",
                                            "path": "",
                                            "collapse": collapse,
                                            "DataList": obj.items,
                                            "NotInitialization": false
                                        }
                                    },
                                    "container": {
                                        "InitVal": 0,
                                        "name": "container",
                                        "List": {
                                            "main": {
                                                "InitVal": 0,
                                                "name": "main",
                                                "Form": {
                                                    "InitVal": 0,
                                                    "name": "transition"

                                                }
                                            },
                                            "footer": {
                                                "InitVal": 0,
                                                "name": "footer",
                                                "height": "60px",
                                                "Form": {}
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    };
                    if (_window.uuid) {
                        var obj = require('./userkey');
                        mainDrive.List.userkey = obj.getPc();
                    }
                    setTimeout(function () {
                        callback(mainDrive);
                    }, 50);
                }
                openMenu(true)
            }
            var cf = {
                domain: "<%domain%>",
                language: '<%language%>',
                drive: {
                    item: '<%extend%>',
                    main: getmain
                }
            };
            return cf;
        };
        if (callback) {
            setTimeout(function () {
                callback(value);
            }, 5000);
        } else {
            return value;
        }
    };

    _pwa_sw(win, option.swdrive);
}

module.exports = pcApp;